<?php
/**
 *
 * 版    本：1.1
 * 功能说明：文章控制器。
 *
 **/

namespace Admin\Controller;

use Vendor\Tree;

class CategoryController extends ComController
{

    public function index($pid=0)
    {
		$map['id']=$pid;
		$map['_string'] = "  concat(',',path,',') like '%,$pid,%'  ";
		$map['_logic'] = 'OR';
        $category = M('category')->where($map)->field('id,pid,name,o')->order('o desc,id asc')->select();
        $category = $this->getMenu($category);
        $this->assign('category', $category);
        $this->assign('pid', $pid);
        $this->display();
    }

    public function del()
    {

        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $data['id'] = $id;
            $category = M('category');
			$data1 = $category->where(array('id'=>$data['id']))->find();
            if ($category->where('pid=' . $id)->count()) {
                die('2');//存在子类，严禁删除。
            }
			elseif($data1['pid']==0){
				die('3');	//一级目录不能删除
			}
			else {

                $data = M('article')->where('sid=' . $id)->select();
				foreach($data as $k=>$v){
					if(file_exists('.'.$v['thumbnail'])){
						unlink('.'.$v['thumbnail']); //删除文件
					}
				}
                addlog('删除分类下文章里的图片，分类ID：' . $id);
                M('article')->where('sid=' . $id)->delete(); //删除分类里的文章
                addlog('删除分类下的文章，分类ID：' . $id);
                $category->where('id=' . $id)->delete();

                addlog('删除分类，ID：' . $id);
            }
            die('1');
        } else {
            die('0');
        }

    }

    public function edit($pid=0,$id=0)
    {

        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        $currentcategory = M('category')->where('id=' . $id)->find();
        $this->assign('currentcategory', $currentcategory);

        $category = M('category')->field('id,pid,name')->where("id <> {$id}")->order('o asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $currentcategory['pid']);

        $this->assign('category', $category);
        $this->assign('pid', $pid);
        $this->assign('id', $id);
        $this->display('form');
    }

    public function add($pid=0)
    {
		$map['id']=$pid;
		$map['_string'] = "  concat(',',path,',') like '%,$pid,%'  ";
		$map['_logic'] = 'OR';

        $pid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
        $category = M('category')->where($map)->field('id,pid,name')->order('o asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $pid);

        $this->assign('category', $category);
        $this->assign('pid', $pid);
        $this->display('form');
    }

    public function update($act = null,$classid=0)
    {
        if ($act == 'order') {
            $id = I('post.id', 0, 'intval');
            if (!$id) {
                die('0');
            }
            $o = I('post.o', 0, 'intval');
            M('category')->data(array('o' => $o))->where("id='{$id}'")->save();
            addlog('分类修改排序，ID：' . $id);
            die('1');
        }

        $id = I('post.id', false, 'intval');
        $data['type'] = I('post.type', 0, 'intval');
        $data['pid'] = I('post.pid', 0, 'intval');
        $data['classid'] = I('post.classid', 0, 'intval');
        $data['name'] = I('post.name');
		$data['dir'] = I('post.dir','',array('strip_tags','trim'));
        $data['seotitle'] = I('post.seotitle', '', 'htmlspecialchars');
        $data['keywords'] = I('post.keywords', '', 'htmlspecialchars');
        $data['description'] = I('post.description', '', 'htmlspecialchars');
        $data['content'] = I('post.content');
        $data['url'] = I('post.url');
        $data['cattemplate'] = I('post.cattemplate');
        $data['contemplate'] = I('post.contemplate');
		$data['thumbnail'] = I('post.thumbnail', '', 'strip_tags');
        $data['o'] = I('post.o', 0, 'intval');
        if ($data['name'] == '') {
            $this->error('分类名称不能为空！');
        }


        if ($id) {
			unset($data['pid']); //修改时不写入PID的值
            if (M('category')->data($data)->where('id=' . $id)->save()) {
                addlog('文章分类修改，ID：' . $id . '，名称：' . $data['name']);
                $this->success('恭喜，分类修改成功！','index.html?pid='.$classid);
                die(0);
        	}
        } else {
			
			$shu   =   M('category');
			if($data['classid'] == C('newsclasszdlm')){
				$data2 = $shu->where(array('id'=>$data['pid']))->find();
				if($data2['deep'] >= C('newsclasszdlmsd')){
					$this->error('本栏目指定深度为'.C('newsclasszdlmsd').'级，不能增加下级栏目','',1);
				}
			}else{
				$data2 = $shu->where(array('id'=>$data['pid']))->find();
				if($data2['deep'] >= C('newsclasssd')){
					$this->error('本栏目指定深度为'.C('newsclasssd').'级，不能增加下级栏目','',1);
				}	
			}
			
			if($data['pid']==0){
				$data['path'] = 0;
                $data['deep']=1;
			}else{
				$data1 = M('category')->where(array('id'=>$data['pid']))->find();
				$data['path'] = $data1['path'].','.$data['pid'];
                $data['deep']=$data1['deep']+1;	
			}
            $id = M('category')->data($data)->add();
            if ($id) {
                addlog('新增分类，ID：' . $id . '，名称：' . $data['name']);
                $this->success('恭喜，新增分类成功！','index.html?pid='.$classid);
                die(0);
            }
        }
        $this->success('恭喜，操作成功！','index.html?pid='.$classid);
    }
}
